Advertisement
Guest User

Untitled

a guest
Dec 7th, 2022
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.27 KB | Source Code | 0 0
  1. #### By: TheMightyFozz ####
  2. bl_info = {
  3.     "name": "OpenFileLocation",
  4.     "description": "Opens saved .blend file in default ",
  5.     "author": "TheMightyFozz",
  6.     "version": (0, 2),
  7.     "blender": (3, 3, 1),
  8.     "location": "View_3D > Open File Browser",
  9.     "warning": "", # used for warning icon and text in addons panel
  10.     "doc_url": "http://wiki.blender.org",
  11.     "tracker_url": "https://developer.blender.org",
  12.     "support": "COMMUNITY",
  13.     "category": "Import-Export",
  14. }
  15. #Import dependencies
  16. import os, bpy, platform, subprocess
  17.  
  18. #Set variables
  19.  
  20.  
  21. ###########The Thing I want the button to do ###############
  22.  
  23. class OpenFL_OT_DirectoryOpen(bpy.types.Operator):
  24.     bl_idname = "cross.directory_open"
  25.     bl_label = "Open Folder" # label needs to be generic between os flavours
  26.     bl_options = {'REGISTER'}
  27.  
  28.     directory: bpy.props.StringProperty()
  29.  
  30.     def execute(self, context):
  31.         if self.directory:
  32.             sysInfo = platform.system()
  33.             print(sysInfo)
  34.             if sysInfo == 'darwin':
  35.                 # If Mac Then use Open
  36.                 subprocess.check_call(['open', '--', self.directory])
  37.             elif sysInfo == 'Linux':
  38.                 # If Linux use xdg-open
  39.                 subprocess.Popen(["xdg-open", self.directory])
  40.             elif sysInfo == 'Windows':
  41.                 # If Windows use explorer
  42.                 os.startfile(self.directory)
  43.  
  44.             return {'FINISHED'}
  45.         else:
  46.             self.report({"ERROR"}, "Missing 'directory' variable")
  47.  
  48. ############################################################
  49.  
  50. #Creates UI Panel
  51. class OpenFL_PT_main_panel(bpy.types.Panel):
  52.     """Opens Current File Location In File Browser"""
  53.     bl_label = "Open File Location"
  54.     bl_idname = "ofl_pt.main_panel"
  55.     bl_space_type = "VIEW_3D"
  56.     bl_region_type = 'UI'
  57.     bl_category = 'Open File Location'
  58.  
  59.     def draw(self, context):
  60.         layout = self.layout
  61.  
  62.         row = layout.row()
  63.         row.label(text= "Open .blend Location", icon= 'FILEBROWSER')
  64.         row = layout.row()
  65.         cross = row.operator("cross.directory_open")
  66.         cross.directory = os.path.dirname(bpy.data.filepath)
  67.  
  68. #Register and Unregister
  69. def register():
  70.     bpy.utils.register_class(OpenFL_PT_main_panel)
  71.     bpy.utils.register_class(OpenFL_OT_DirectoryOpen)
  72.  
  73. def unregister():
  74.     bpy.utils.unregister_class(OpenFL_PT_main_panel)
  75.     bpy.utils.unregister_class(OpenFL_OT_DirectoryOpen)
  76.  
  77. if __name__ == "__main__":
  78.     register()
  79.  
  80.  
  81. ################Notes from V 0.1 ###################
  82.     #maybe issues with windows
  83.     #this code may help
  84.     #open_wrapper(os.path.dirname(bpy.data.filepath))
  85.     #Could streamline code
  86.     #(if you want to make your operator more modular you can also set it as a prop and use that in draw:
  87.     #row.operator("findandopen.run",text ="Open Folder").directory = os.path.dirname(bpy.data.filepath)
  88.  
  89.    
  90. ################Notes For V 0.3  ###################
  91.     # @melmass
  92.     # Technically though it's not ideal that the os.path operation happens in draw,
  93.     # but it was to give you the context of how to add prop to operators, it would be more efficient
  94.     # in this case to keep it in execute so it's only run when you ask it and not every time it's on screen
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement